﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-09-25 11:20:30
 * 功能：地图管理
**************************************************************************************/

using Game.Framework;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

namespace Game.UI.HallPanel
{
    public class DairyView : MonoBehaviour, ILineObj
    {
        private DraggableMap m_Drapgable;
        private CityItem[] m_CityItems;
        private UILineRenderer m_Line;

        private void Awake()
        {
            m_Drapgable = transform.Find("Map").GetComponent<DraggableMap>();
            m_CityItems = m_Drapgable.GetComponentsInChildren<CityItem>();
            PoolManager.Instance.CreatePool(HallPanelNotifications.FOOT_ITEM_POOL, transform.Find("FootItem").gameObject, 16);
            m_Line = m_Drapgable.transform.Find("LineMgr").GetComponent<UILineRenderer>();
            EventCenter.Instance.AddEventListen<string>(GameEventName.OnCityUnlock, OnCityUnlock);
            m_Line.Init(this);
        }

        public void OnEnable()
        {
            CityItem curCityProgress = null;
            var cities = GameDataMgr.Instance.MapInfo[GameDataMgr.Instance.MapInfo.currentCountry].cities.Values.ToList();
            for (int i = 0; i < m_CityItems.Length; i++)
            {
                m_CityItems[i].Init(cities[i]);
                if (cities[i].name == GameDataMgr.Instance.MapInfo.currentCity)
                    curCityProgress = m_CityItems[i];
            }

            // 将当前进度居中显示
            m_Drapgable.FocusOnLocationLocal((curCityProgress.transform as RectTransform).anchoredPosition);

            OnCityUnlock("");
        }

        private void OnCityUnlock(string cityName)
        {
            var country = GameDataMgr.Instance.MapInfo[GameDataMgr.Instance.MapInfo.currentCountry];
            List<Vector2> points = new List<Vector2>(country.unlockedCities.Count);
            for (int i = 0; i < country.unlockedCities.Count; i++)
            {
                var cityCfg = BaseDataMgr.Instance.GetCountryCfg(country.name)[country.unlockedCities[i]];
                points.Add(new Vector2(cityCfg.posX, cityCfg.posY));
            }
            m_Line.DrawLine(points, true);
        }

        private void OnDestroy()
        {
            EventCenter.Instance.RemoveEventListen<string>(GameEventName.OnCityUnlock, OnCityUnlock);
            PoolManager.Instance.ClearPool(HallPanelNotifications.FOOT_ITEM_POOL);
        }

        public Image Get()
        {
            var obj = PoolManager.Instance.Get(HallPanelNotifications.FOOT_ITEM_POOL);
            return obj.GetComponent<Image>();
        }

        public void Delete(GameObject line)
        {
            PoolManager.Instance.Delete(HallPanelNotifications.FOOT_ITEM_POOL, line);
        }

        public void OpenFirst()
        {
            StartCoroutine(DelayOpen());
        }

        private IEnumerator DelayOpen()
        {
            yield return new WaitForSeconds(0.1f);
            m_CityItems[0].ClickCityBtn();
        }
    }
}